package com.ruoyi.zhjj.service.impl;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.enums.DelStatus;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.ApiResponse;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhjj.mapper.ZhjjContractorInfoMapper;
import com.ruoyi.zhjj.domain.ZhjjContractorInfo;
import com.ruoyi.zhjj.service.IZhjjContractorInfoService;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

/**
 * 承包商信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class ZhjjContractorInfoServiceImpl implements IZhjjContractorInfoService
{
    @Autowired
    private ZhjjContractorInfoMapper zhjjContractorInfoMapper;

    @Autowired
    private Validator validator;

    /**
     * 查询承包商信息
     *
     * @param id 承包商信息主键
     * @return 承包商信息
     */
    @Override
    public ZhjjContractorInfo selectZhjjContractorInfoById(Long id)
    {
        return zhjjContractorInfoMapper.selectZhjjContractorInfoById(id);
    }

    /**
     * 查询承包商信息列表
     *
     * @param zhjjContractorInfo 承包商信息
     * @return 承包商信息
     */
    @Override
    public List<ZhjjContractorInfo> selectZhjjContractorInfoList(ZhjjContractorInfo zhjjContractorInfo)
    {
        return zhjjContractorInfoMapper.selectZhjjContractorInfoList(zhjjContractorInfo);
    }

    /**
     * 新增承包商信息
     *
     * @param zhjjContractorInfo 承包商信息
     * @return 结果
     */
    @Override
    public int insertZhjjContractorInfo(ZhjjContractorInfo zhjjContractorInfo)
    {
//        // 使用Validator进行验证
//        Set<ConstraintViolation<ZhjjContractorInfo>> violations = validator.validate(zhjjContractorInfo);
//        if (!violations.isEmpty()) {
//            // 获取第一个错误信息
//            String errorMessage = violations.iterator().next().getMessage();
//            throw new ServiceException(errorMessage,HttpStatus.ERROR);
//        }
        zhjjContractorInfo.setCreateTime(DateUtils.getNowDate());
        zhjjContractorInfo.setCreateBy(SecurityUtils.getUsername());
        zhjjContractorInfo.setDelFlag(DelStatus.EXITS.getCode());
        // 重复数据验证
        try{
            ZhjjContractorInfo existingInfo = selectZhjjContractorInfoByExtSysId(zhjjContractorInfo.getExtSysId());
            if (existingInfo != null) {
                throw new ServiceException("源数据表ID已存在：", HttpStatus.DATA_EXIST);
            }
            return zhjjContractorInfoMapper.insertZhjjContractorInfo(zhjjContractorInfo);

            //        TODO 将新增消息传入到RabbitMQ
        }catch (Exception e){
            throw new ServiceException("操作失败：" + e.getMessage(), HttpStatus.ERROR);
        }
    }

    /**
     * 修改承包商信息
     *
     * @param zhjjContractorInfo 承包商信息
     * @return 结果
     */
    @Override
    public int updateZhjjContractorInfo(ZhjjContractorInfo zhjjContractorInfo)
    {
        //        TODO 将新增消息传入到RabbitMQ
        zhjjContractorInfo.setUpdateTime(DateUtils.getNowDate());
        zhjjContractorInfo.setUpdateBy(SecurityUtils.getUsername());
        return zhjjContractorInfoMapper.updateZhjjContractorInfo(zhjjContractorInfo);
    }

    /**
     * 批量删除承包商信息
     *
     * @param ids 需要删除的承包商信息主键
     * @return 结果
     */
    @Override
    public int deleteZhjjContractorInfoByIds(Long[] ids)
    {
        //        TODO 将新增消息传入到RabbitMQ
        return zhjjContractorInfoMapper.deleteZhjjContractorInfoByIds(ids);
    }

    /**
     * 删除承包商信息信息
     *
     * @param id 承包商信息主键
     * @return 结果
     */
    @Override
    public int deleteZhjjContractorInfoById(Long id)
    {
        return zhjjContractorInfoMapper.deleteZhjjContractorInfoById(id);
    }

    @Override
    public ZhjjContractorInfo selectZhjjContractorInfoByExtSysId(int extSysId) {
        return zhjjContractorInfoMapper.selectZhjjContractorInfoByExtSysId(extSysId);
    }
}
